package problems.daily;

/**
 * 777. 在LR字符串中交换相邻字符
 * <p>https://leetcode.cn/problems/swap-adjacent-in-lr-string/</p>
 *
 * @author habitplus
 * @since 9:58, 2022/10/2
 */
public class DT777 {
    // 双指针
    public boolean canTransform(String start, String end) {
        char[] stChs = start.toCharArray();
        char[] edChs = end.toCharArray();
        int n = stChs.length;
        int i = 0, j = 0;

        while (i < n && j < n) {
            while (i < n && stChs[i] == 'X') i++;

            while (j < n && edChs[j] == 'X') j++;

            if (i < n && j < n) {
                if (stChs[i] != edChs[j]) return false;
                if ((stChs[i] == 'L' && i < j) || (stChs[i] == 'R' && i > j)) return false;

                i++;
                j++;
            }
        }

        while (i < n) {
            if (stChs[i] != 'X') return false;
            i++;
        }

        while (j < n) {
            if (edChs[j] != 'X') return false;
            j++;
        }

        return true;
    }
}
